/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.example.controller;

import org.example.service.NestedThreadsExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 *
 * @author gino
 */
@Controller
@RequestMapping("/SimpleController")
public class SimpleController {

    @Value("${spring.application.name}")
    String appName;

    @Autowired
    private Test test;

    private final AtomicBoolean stopAllThreads = new AtomicBoolean(false);


    @Autowired
    private NestedThreadsExampleService nestedThreadsExampleService;

//    @GetMapping("/homePage")
//    public String homePage() {
//        test.asyncExecutor().execute(() -> {
//
//            System.out.printf("我是异步子线程");
//        });
////        model.addAttribute("appName", appName);
//        return "home";
//    }

    @GetMapping("/test")
    public String test() {
        ExecutorService executor = Executors.newFixedThreadPool(6);
        int finalI = 0;
        for (int i = 0; i < 12; i++) {
            finalI = i;
            int finalI1 = finalI;
            executor.submit(() -> {
                if (!stopAllThreads.get()) {
                    System.out.println("Outer thread " + Thread.currentThread().getName() + " is running");
                    // 外部线程的任务代码，连接数据库等
                    long zhuId = Thread.currentThread().getId();
                    System.out.println("----------11111111主线程id" + zhuId + "标识:" + finalI1);

                    ExecutorService innerExecutor = Executors.newFixedThreadPool(2);
                    for (int j = 0; j < 6; j++) {
                        innerExecutor.submit(() -> {
                            nestedThreadsExampleService.extracted(finalI1, zhuId, stopAllThreads);
                        });
                    }
                    System.out.println("----------主线程id" + zhuId);
                    innerExecutor.shutdown();
                    try {
                        innerExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        System.out.println("实际执行了几次：" + finalI);
        executor.shutdown();
        try {
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("最终的结果是:"+stopAllThreads.get());
        return "success";
    }

}
